import logging
from logging.handlers import TimedRotatingFileHandler
import os
from datetime import datetime

# 获取当前日期并格式化为字符串
current_date = datetime.now().strftime('%Y_%m_%d')

# 拼接日志文件名
log_file_name = f'app_{current_date}.log'

# 定义日志文件的保存目录
log_dir = 'logs'
if not os.path.exists(log_dir):
    os.makedirs(log_dir)

# 不同类型的日志写入不同的文件
log_file_names = {
    logging.INFO: f'info_{current_date}.log',
    logging.WARNING: f'warning_{current_date}.log',
    logging.ERROR: f'error_{current_date}.log',
    logging.CRITICAL: f'critical_{current_date}.log'
}

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)  # 设置日志记录器的级别为INFO

# 创建一个按天轮换的日志处理器，并指定日志文件的保存位置
for level, log_file_name in log_file_names.items():
    log_file_path = os.path.join(log_dir, log_file_name)
    log_handler = TimedRotatingFileHandler(log_file_path, when='midnight', backupCount=7)
    log_handler.suffix = '%Y-%m-%d.log'
    log_handler.setLevel(level)
    # 设置日志格式
    formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
    log_handler.setFormatter(formatter)
    logger.addHandler(log_handler)

# 使用方法
# from utils.logger import logger
# logger.info(f"Processed market data event with {args.stock_code} records.")
# logger.warning(f"Processed market data event with {args.stock_code} records.")
# logger.error(f"Processed market data event with {args.stock_code} records.")
# logger.critical(f"Processed market data event with {args.stock_code} records.")